package com.ebt.m.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ebt.m.adapter.AccordionAdapter;

/**
 * 手风琴改版
 *
 * @author allen.li
 */
public class Accordion extends LinearLayout {
    private int mCurrentExpandPosition = -1;// 当前展开的索引
    /**
     * AccordionAdapter，数据适配器 1.获取数据 2.修改数据 3.刷新数据
     */
    private AccordionAdapter<?> mAdapter;

    public Accordion(Context context) {
        this(context, null);
    }

    public Accordion(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Accordion(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setOrientation(LinearLayout.VERTICAL);
    }

    /**
     * @param adapter expandPosition 展开的索引 从0开始
     */
    public void setAdapter(AccordionAdapter<?> adapter, int expandPosition) {
        removeAllViews();
        mAdapter = adapter;
        for (int i = 0, j = adapter.getCount(); i < j; i++) {
            LinearLayout wrapper = getWrapper(i == expandPosition);
            View header = adapter.getHeaderView(i, wrapper);
            View content = adapter.getContentView(i, wrapper);
            header.setTag(i);
            content.setVisibility(View.GONE);
            content.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            wrapper.addView(header);
            wrapper.addView(content);
            header.setOnClickListener(clickListener);
            wrapper.setVisibility(adapter.getVisibility(i));
            addView(wrapper);
        }
        expand(expandPosition);
    }

    public AccordionAdapter<?> getAdapter() {
        return mAdapter;
    }

    /**
     * 展开第几个
     *
     * @param position
     */
    public void expand(int position) {
        if (mAdapter.ifCanExpand(position)) {
            if (mCurrentExpandPosition >= 0) {// 收起当前展开项目
                LinearLayout currentWrapper = (LinearLayout) getChildAt(mCurrentExpandPosition);
                setWrapperLayoutParams(currentWrapper, false);
                mAdapter.onCollapsed(mCurrentExpandPosition, currentWrapper.getChildAt(0));
            }
            // 展开选中项
            mCurrentExpandPosition = position;
            LinearLayout focusedWrapper = (LinearLayout) getChildAt(mCurrentExpandPosition);
            setWrapperLayoutParams(focusedWrapper, true);
            mAdapter.onExpanded(mCurrentExpandPosition, focusedWrapper.getChildAt(0));
        }

    }

    /**
     * 获得GroupView 如果不存在则返回null
     *
     * @param groupIndex 索引 从0开始
     * @return
     */
    public View getGroupView(int groupIndex) {
        if (groupIndex < getChildCount()) {
            LinearLayout wrapper = (LinearLayout) getChildAt(groupIndex);
            return wrapper.getChildAt(0);
        } else {
            return null;
        }
    }

    /**
     * 获取第几组的头部view
     *
     * @param group
     * @return
     */
    public ViewGroup getContentView(int group) {
        if (group < getChildCount()) {
            LinearLayout wrapper = (LinearLayout) getChildAt(group);
            return (ViewGroup) wrapper.getChildAt(1);
        } else {
            return null;
        }
    }

    /**
     * 设置可见性
     *
     * @param position
     */
    public void setVisibility(int position, boolean visible) {
        LinearLayout wrapper = (LinearLayout) getChildAt(position);
        if (visible) {
            wrapper.setVisibility(View.VISIBLE);
        } else {
            wrapper.setVisibility(View.GONE);
        }
    }

    /**
     * 当前展开的索引
     *
     * @return
     */
    public int getExpandPosition() {
        return mCurrentExpandPosition;
    }

    /**
     * 清除展开项目
     *
     * @return
     */
    public void clearExpandPosition() {
        mCurrentExpandPosition = -1;
    }

    /**
     * 手风琴点击事件监听 触发收起项目和展开项目动作
     */
    private OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = Integer.parseInt(v.getTag().toString());
//            if (mCurrentExpandPosition == position) {
//                if (mCurrentExpandPosition >= 0) {// 收起当前展开项目
//                    LinearLayout currentWrapper = (LinearLayout) getChildAt(mCurrentExpandPosition);
//                    setWrapperLayoutParams(currentWrapper, false);
//                    mAdapter.onCollapsed(mCurrentExpandPosition, currentWrapper.getChildAt(0));
//                }
//            } else {
//                expand(position);
//            }
            toggle(position);
        }
    };

    /**
     * 收缩当前项目
     *
     * @param position
     */
    private void collapse(int position) {
        LinearLayout currentWrapper = (LinearLayout) getChildAt(position);
        setWrapperLayoutParams(currentWrapper, false);
        mAdapter.onCollapsed(position, currentWrapper.getChildAt(0));
        mCurrentExpandPosition = -1;
    }

    private void toggle(int position) {
        if (mCurrentExpandPosition == position) {//当前展开
            collapse(position);
        }else{
            expand(position);
        }
    }

    /**
     * 获得每一项的WrapperView容器，用于添加header和content
     *
     * @param isExpanded 是否展开
     * @return
     */
    private LinearLayout getWrapper(boolean isExpanded) {
        LinearLayout wrapper = new LinearLayout(getContext());
        setWrapperLayoutParams(wrapper, isExpanded);
        wrapper.setOrientation(LinearLayout.VERTICAL);
        return wrapper;
    }

    /**
     * 设置WrapperView容器的LayoutParams，收起或展开项目
     *
     * @param wrapper
     * @param isExpanded 是否展开
     */
    private void setWrapperLayoutParams(LinearLayout wrapper, boolean isExpanded) {
        LayoutParams params;
        if (isExpanded) {
            params = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
            params.weight = 1;
        } else {
            params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.weight = 0;
        }
        wrapper.setLayoutParams(params);
    }
}
